package com.zzl.finance.data.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.constant.BusinessModeEnum;
import com.zzl.finance.data.constant.SealTypeEnum;
import com.zzl.finance.data.dto.*;
import com.zzl.finance.data.entity.FinancialProductEntity;
import com.zzl.finance.data.service.FileService;
import com.zzl.finance.data.service.FinancialProductService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Map;

import static com.zzl.finance.data.constant.FileBucketTypeConstant.FILE_BUCKET_FINANCING_RELATED_FILE;

/**
 * 金融产品
 *
 * @author wztsl
 * @date 2021-12-22 16:56:38
 */
@ApidocController("金融产品")
@RestController
@RequestMapping("data/financialproduct")
@Slf4j
@SuppressWarnings("unused")
public class FinancialProductController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private FinancialProductService financialProductService;
    @SuppressWarnings("all")
    @Autowired
    private FileService fileService;

    @GetMapping("/list")
    @ApidocOperation(description = "获取金融产品列表数据", operationId = "data/financialproduct/list1")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "list", valueClass = FinancialProductDto.class)
    })
    public Result list() {
        return Result.ok().put("list", financialProductService.getFinancialProductList(this.getUser(),null));
    }

    @GetMapping("plat/list/{enterpriseId}")
    @ApidocOperation(description = "平台-获取金融产品列表数据", operationId = "data/plat/financialproduct/list1")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "list", valueClass = FinancialProductDto.class)
    })
    public Result platList(@ApidocParam(name = "enterpriseId", description = "企业id", required = true)
                           @PathVariable("enterpriseId") String enterpriseId) {
        return Result.ok().put("list", financialProductService.getFinancialProductList(this.getChangeEnterpriseUser(enterpriseId),enterpriseId));
    }

    /**
     * 列表
     */
    @GetMapping("/productList")
    @ApidocOperation(description = "分页查询金融产品数据", operationId = "data/financialproduct/list")
    @ApidocReturnPage(genericTypeClass = FinancialProductListDto.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = "keywords", keyDescription = "金融机构") @RequestParam Map<String, Object> params) {
        PageUtils page = financialProductService.queryPage(params);
        return Result.ok().put("page", page);
    }

    @GetMapping("/businessModeList")
    @ApidocOperation(description = "业务方式下拉框", operationId = "data/financialproduct/businessModeList")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "list", valueClass = KeyValueDto.class)
    })
    public Result businessModeList() {
        return Result.ok().put("list", BusinessModeEnum.getBusinessModeList());
    }

    @GetMapping("/sealTypeList")
    @ApidocOperation(description = "用章类型下拉框", operationId = "data/financialproduct/sealTypeList")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "list", valueClass = KeyValueDto.class)
    })
    public Result sealTypeList() {
        return Result.ok().put("list", SealTypeEnum.getSealTypeList());
    }

    /**
     * 保存
     */
//    @PostMapping("/save")
    @ApidocOperation(description = "保存金融产品", operationId = "data/financialproduct/save")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的金融产品对象值", required = true) @RequestBody @Validated FinancialProductAddDto financialProduct) {
        try {
            financialProductService.saveFinancialProduct(financialProduct, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApidocOperation(description = "修改金融产品", operationId = "data/financialproduct/update")
    @ApidocReturnR
    public Result update(@ApidocParam(name = "修改的金融产品对象值", required = true) @RequestBody FinancialProductUpdateDto financialProduct) {
        try {
            financialProductService.updateFinancialProductById(financialProduct, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 信息
     * 暂时没有维护金融产品的入口，金融产品维护接口暂时都取消掉
     */
    @ApidocOperation(description = "通过id获取金融产品数据", operationId = "data/financialproduct/info")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "financialProduct", valueClass = FinancialProductEntity.class)
    })
    public Result info(@ApidocParam(name = "金融产品id", required = true) @PathVariable("id") Integer id) {
        FinancialProductEntity financialProduct = financialProductService.getById(id);
        return Result.ok().put("financialProduct", financialProduct);
    }


    /**
     * 删除
     * 暂时没有维护金融产品的入口，金融产品维护接口暂时都取消掉
     */
    @ApidocOperation(description = "删除金融产品", operationId = "data/financialproduct/delete")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的金融产品对象id集合", required = true) @RequestBody Integer[] ids) {
        if (ids != null && ids.length > 0) {
            financialProductService.removeByIds(Arrays.asList(ids));
            return Result.ok();
        }
        return Result.error("没有选择要删除的对象！");
    }

    /**
     * 融资相关附件上传
     *
     * @param file 对象
     */
    @PostMapping("/financial/uploadFile")
    @ApidocOperation(description = "融资相关附件上传", operationId = "data/financialproduct/financial/uploadFile")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = String.class)
    })
    public Result upLoadFile(@ApidocParam(name = "file", description = "文件") @NotNull MultipartFile file) {
        try {
            String objId = fileService.upLoadFile(file, FILE_BUCKET_FINANCING_RELATED_FILE.getKey());
            return Result.ok().data(objId);
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }
}
